/**
 * Created by Administrator on 2018/5/10.
 */
define(function (require,exports,module) {
    require("./enter.css");
    require("../../../lib/fullcalendar/moment.min.js");
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        props: ['parentData'],
        template: sTpl,
        data:function () {
            return {
                type:"normal",
                planes:[],
                planesMap:{},
                fileInputDestroy:false,
                excelDate:[],
                excelView:false,
                xlf:{},
                xlfName:"",
                jsLoaded:false,
                flightInfo:{}
            };
        },
        methods:{
            xlFileChange:function (e) {
                this.fileInputDestroy=true;
                this.xlfName = e.target.value;
                this.xlf = e.target.files;
                this.do_file(this.xlf);
            },
            cancelImport:function () {
                if(this.parentData.cancelCallback){
                    this.parentData.cancelCallback();
                }
            },
            batchSave:function () {
                var $this= this;

                axios.post(basepath+"/api/flightPlane/batchSave",$this.excelDate).then(function (response) {
                    if($this.parentData.callback){
                        $this.parentData.callback();
                    }
                    $this.$notify(
                        {title:'保存成功', message:'航班计划保存成功'});
                        $this.parentData.editCallback();
                })
            },
            batchSaveAndPublish:function () {

                var $this= this;
                $this.excelDate.forEach(function (data) {
                   data.planeStatus="发布";
                })
                axios.post(basepath+"/api/flightPlane/batchSave",$this.excelDate).then(function (response) {
                    $this.$notify(
                        {title:'保存成功', message:'航班计划保存成功'});
                    if($this.parentData.callback){
                        $this.parentData.callback();
                    }
                });

            },
            do_file:function (files) {
                if(!this.jsLoaded){
                    loadJs();
                }
                var rABS = typeof FileReader !== "undefined" && (FileReader.prototype||{}).readAsBinaryString;
                var f = files[0];
                var reader = new FileReader();
                var $this =this;
                reader.onload = function(e) {
                    try{
                        if(typeof console !== 'undefined') console.log("onload", new Date(), rABS);

                        var data = e.target.result;
                        var wb = XLS.read(data, {type: rABS ? 'binary' : 'array'});
                        if(!rABS) data = new Uint8Array(data);
                        var ws = wb.Sheets[wb.SheetNames[0]];
                        var data = XLSX.utils.sheet_to_json(ws, {header:1});



                        var a=['flightInfo','normalAbnormality','estimatedTime',
                            'importOrExport','originating','destination','takeOffTime','arriveTime'];
                        var b=['航班号','预约日期',
                            '进出港','出发地','目的地','计划起飞时间','计划到达时间']
                        $this.excelDate=[];
                        for(var i=1;i<data.length;i++){
                            $this.excelDate[i]={};
                            for(var n in a){
                                $this.excelDate[i][a[n]] = data[i][n];
                                if(!data[i][n]){
                                    $this.$notify({title:"excel内容异常",message:"字段 "+b[n]+" 不能为空"});
                                }
                            }
                            $this.excelDate[i]['planeStatus']="新建";
                            $this.excelDate[i].flightInfo=$this.planesMap[$this.excelDate[i].flightInfo];
                            $this.excelDate[i].planeProperty = $this.parentData.planeProperty;
                            try{
                                $this.excelDate[i].takeOffTime =  moment(new Date($this.excelDate[i].takeOffTime)).format("YYYY-MM-DD HH:mm");
                                $this.excelDate[i].arriveTime  = moment(new Date($this.excelDate[i].arriveTime)).format("YYYY-MM-DD HH:mm");
                                $this.excelDate[i].estimatedTime  = moment(new Date($this.excelDate[i].estimatedTime)).format("YYYY-MM-DD");
                            }catch(e) {
                                $this.$notify({title:"日期格式异常",message:"请检查文件内容"});
                            }
                        }
                        $this.excelDate.splice(0,1);
                        $this.excelView = true;
                    }catch (e){
                       $this.$notify({title:"文件格式异常",message:"请检查文件内容"});
                    }
                    $this.fileInputDestroy=false;
                };
                if(rABS) reader.readAsBinaryString(f);
                else reader.readAsArrayBuffer(f);
            },
            loadJs:function () {
                var $this = this;
                require.async("../../../lib/js-xlsx-master/shim.js",function (data) {
                    require.async("../../../lib/js-xlsx-master/xlsx.full.min.js", function (data) {
                        $this.jsLoaded=true;
                    });
                });
            },querySearchPlace:function(queryString, cb) {
                var restaurants = this.citys;
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },

        },
        created:function (){
            var $this=this;
            this.loadJs();
            axios.get(basepath+"/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planesMap={};
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                    $this.planesMap[plane.flightNum] = plane;
                })
            })

    },
        activated:function () {
            if(!this.parentData){
                messages.$emit('change','航班计划Excel录入');
            }
        }

    })
    module.exports = VueComponent;

    return VueComponent;
});
